/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.autocode.manager.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.ksplatform.base.BaseController;
import cn.com.ksplatform.base.Return;
import cn.com.ksplatform.base.SimpleReturn;
import cn.com.ksplatform.base.service.BaseService;
import cn.com.ksplatform.modules.autocode.dto.ColumnArray;
import cn.com.ksplatform.modules.autocode.dto.Events;
import cn.com.ksplatform.modules.autocode.dto.Propertys;
import cn.com.ksplatform.modules.autocode.manager.model.Sence;
import cn.com.ksplatform.modules.autocode.manager.service.SenceService;

/**
 * ClassName:SenceController.java <br/>
 * Date: 2015年11月5日 <br/>
 * 
 * @author Mr.Xiao
 * @version 1.0
 */
@Controller
@RequestMapping("/modules/autocode/sence")
public class SenceController extends BaseController{
	
	private SenceService senceService;
	
	@Resource(name = "modules.autocode.SenceService")
	@Override
	public void setBaseService(BaseService baseService) {
		senceService = (SenceService) baseService;
		super.setBaseService(baseService);
	}
	
	@RequestMapping("saveSence")
	@ResponseBody
	public Return saveSence(Sence sence){
		Return rtn = null;
		try {
			senceService.saveSence(sence);
			rtn = SimpleReturn.success("操作成功");
		} catch (Exception e) {
			rtn = SimpleReturn.error("操作失败:"+e.getMessage());
			e.printStackTrace();
		}
		return rtn;
	}
	
	@RequestMapping("updateSence")
	@ResponseBody
	public Return updateSence(Sence sence){
		Return rtn = null;
		try {
			senceService.updateSence(sence);
			rtn = SimpleReturn.success("操作成功");
		} catch (Exception e) {
			rtn = SimpleReturn.error("操作失败:"+e.getMessage());
			e.printStackTrace();
		}
		return rtn;
	}
	
	//通用
	@RequestMapping("addPropertysToSence")
	@ResponseBody
	public Return addPropertysToSence(Sence sence,Propertys propertys){
		Return rtn = null;
		try {
			senceService.addPropertysToSence(sence,propertys);
			rtn = SimpleReturn.success("操作成功");
		} catch (Exception e) {
			rtn = SimpleReturn.error("操作失败:"+e.getMessage());
			e.printStackTrace();
		}
		return rtn;
	}
	
	@RequestMapping("addEventsToSence")
	@ResponseBody
	public Return addEventsToSence(Sence sence,Events events){
		Return rtn = null;
		try {
			senceService.addEventsToSence(sence, events);
			rtn = SimpleReturn.success("操作成功");
		} catch (Exception e) {
			rtn = SimpleReturn.error("操作失败:"+e.getMessage());
			e.printStackTrace();
		}
		return rtn;
	}
	
	@RequestMapping("getPropertysBySence")
	@ResponseBody
	public Return getPropertysBySence(Sence sence){
		Return rtn = null;
		try {
			rtn = SimpleReturn.success("操作成功",senceService.getPropertysBySence(sence).getPropertys());
		} catch (Exception e) {
			rtn = SimpleReturn.error("操作失败:"+e.getMessage());
			e.printStackTrace();
		}
		return rtn;
	}
	@RequestMapping("getEventsBySence")
	@ResponseBody
	public Return getEventsBySence(Sence sence){
		Return rtn = null;
		try {
			rtn = SimpleReturn.success("操作成功",senceService.getEventsBySence(sence).getEvents());
		} catch (Exception e) {
			rtn = SimpleReturn.error("操作失败:"+e.getMessage());
			e.printStackTrace();
		}
		return rtn;
	}
	
	//---------------------------单表-----------------------
	@RequestMapping("addCloumnsToSence")
	@ResponseBody
	public Return addCloumnsToSence(Sence sence,ColumnArray columnArray){
		Return rtn = null;
		try {
			senceService.addCloumnsToSence(sence,columnArray);
			rtn = SimpleReturn.success("操作成功");
		} catch (Exception e) {
			rtn = SimpleReturn.error("操作失败:"+e.getMessage());
			e.printStackTrace();
		}
		return rtn;
	}
	
	@RequestMapping("getCloumnsBySence")
	@ResponseBody
	public Return getCloumnsBySence(Sence sence){
		Return rtn = null;
		try {
			rtn = SimpleReturn.success("操作成功",senceService.getCloumnsBySence(sence));
		} catch (Exception e) {
			rtn = SimpleReturn.error("操作失败:"+e.getMessage());
			e.printStackTrace();
		}
		return rtn;
	}
	
	//---------------------------主子表-----------------------
	@RequestMapping("addCloumnsToSence1_MainTable")
	@ResponseBody
	public Return addCloumnsToSence1_MainTable(Sence sence,ColumnArray columnArray){
		Return rtn = null;
		try {
			senceService.addCloumnsToSence(sence,columnArray,"MainTable.xml");
			rtn = SimpleReturn.success("操作成功");
		} catch (Exception e) {
			rtn = SimpleReturn.error("操作失败:"+e.getMessage());
			e.printStackTrace();
		}
		return rtn;
	}
	
	@RequestMapping("addCloumnsToSence1_ChildrenTable")
	@ResponseBody
	public Return addCloumnsToSence1_ChildrenTable(Sence sence,ColumnArray columnArray,String childrenId){
		Return rtn = null;
		try {
			senceService.addCloumnsToSence(sence,columnArray,"children_"+childrenId+".xml");
			rtn = SimpleReturn.success("操作成功");
		} catch (Exception e) {
			rtn = SimpleReturn.error("操作失败:"+e.getMessage());
			e.printStackTrace();
		}
		return rtn;
	}
	
	@RequestMapping("getCloumnsToSence1_MainTable")
	@ResponseBody
	public Return addCloumnsToSence1_MainTable(Sence sence){
		Return rtn = null;
		try {
			rtn = SimpleReturn.success("操作成功",senceService.getCloumnsBySence(sence,"MainTable.xml"));
		} catch (Exception e) {
			rtn = SimpleReturn.error("操作失败:"+e.getMessage());
			e.printStackTrace();
		}
		return rtn;
	}
	
	@RequestMapping("getCloumnsToSence1_ChildrenTable")
	@ResponseBody
	public Return addCloumnsToSence1_ChildrenTable(Sence sence){
		Return rtn = null;
		try {
			rtn = SimpleReturn.success("操作成功",senceService.getChildrenCloumnsBySence(sence,"children"));
		} catch (Exception e) {
			rtn = SimpleReturn.error("操作失败:"+e.getMessage());
			e.printStackTrace();
		}
		return rtn;
	}
	
	@RequestMapping("removeCloumnsToSence1_ChildrenTable")
	@ResponseBody
	public Return removeCloumnsToSence1_ChildrenTable(Sence sence, @RequestParam(required=true)String childrenId){
		Return rtn = null;
		try {
			senceService.removeCloumnsToSence1_ChildrenTable(sence,"children_"+childrenId+".xml");
			rtn = SimpleReturn.success("操作成功");
		} catch (Exception e) {
			rtn = SimpleReturn.error("操作失败:"+e.getMessage());
			e.printStackTrace();
		}
		return rtn;
	}
}
